package com.google
{
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.URLLoader;
	import mx.controls.Alert;
	import mx.formatters.SwitchSymbolFormatter;
	
	public class Authentication
	{
		private const link : String = "https://www.google.com/accounts/ClientLogin";
		
		private var _email : String;
		private var _password : String;
		private var _token : String;
		
		//constructor
		public function Authentication(emailParam : String, passwordParam : String)
		{
			email = emailParam;
			password = passwordParam;
			token = new String();
		}
		
		//getters
		public function get email (): String
		{
			return _email;
		}
		
		public function get password ():String
		{
			return _password;			
		}

		public function get token ():String
		{
			return _token;			
		}		
		
		//setters
		public function set email (param:String):void
		{
			this._email = param;
		}
		
		public function set password (param:String):void
		{
			this._password = param;
		}

		public function set token (param:String):void
		{
			this._token = param;
		}		
		
		
		
		//Functions
		
		public function authenticate():Boolean
		{
			var urlRequest : URLRequest  = new URLRequest(link);  
			urlRequest.method = URLRequestMethod.POST;
			var variables : URLVariables = new URLVariables;
			variables.accountType = "HOSTED_OR_GOOGLE";
			if ((email!=null)&&(password!=null))
			{
				variables.Email = "willeeh@gmail.com";
				variables.Passwd = "punkbuster";
			}else
				return false;			
			variables.service = "wise"; //http://ruscoe.net/google/google-account-service-names/
			variables.source = "GuillermoCeballos-QuinielaApollo-1.0";						
			urlRequest.data = variables;	
            var ldr:URLLoader = new URLLoader ();
            ldr.load(urlRequest);
            ldr.addEventListener (Event.COMPLETE,onLoad); 
        	ldr.addEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        	ldr.addEventListener (HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        	ldr.addEventListener (IOErrorEvent.IO_ERROR, ioErrorHandler);            
            return true;  	
		}
		
		private function onLoad(event:Event):void
		{		
			switch (event.target.data)
			{
				case "BadAuthentication" :
				case "NotVerified" :
				case "TermsNotAgreed" :	
				case "CaptchaRequired" :	
				case "Unknown" :	
				case "AccountDeleted" :	
				case "AccountDisabled" :	
				case "ServiceDisabled" :	
				case "ServiceUnavailable" :	
					Alert.show(event.target.data);	
					return;																																								
			}
			var received : String = event.target.data;					
			var pos : int = received.indexOf("Auth=",0);
			token = received.substring(pos+5,received.length-1);
			var list : com.google.Lists = new Lists(token);
			list.getListSpreadsheets();			
		}	

		private function securityErrorHandler (event : SecurityErrorEvent) : void
		{
		        Alert.show("securityErrorHandler: " + event);
		}
		private function httpStatusHandler (event : HTTPStatusEvent) : void
		{
		       trace("httpStatusHandler: " + event);
		}
		private function ioErrorHandler (event : IOErrorEvent) : void
		{
		       trace("ioErrorHandler: " + event);
		}
			                        		
	}
}